package com.sino.uam.support.servlet;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.ServletException;

import com.caucho.hessian.server.HessianServlet;
import com.sino.uam.entity.Department;
import com.sino.uam.entity.DptUserRel;
import com.sino.uam.entity.Role;
import com.sino.uam.entity.UserInfo;
import com.sino.uam.entity.UserRoleRel;
import com.sino.uam.rbac.IUser;
import com.sino.uam.rbac.model.Privilege;
import com.sino.uam.rbac.model.Resource;
import com.sino.uam.support.UAMDBManager;
import com.sino.uam.support.UAMDataSource;
import com.sino.uam.support.UAMFascade;
import com.sinodata.bsm.center.purview.FunctionConstants;
import com.sinodata.bsm.center.purview.UAMRequest;
import com.sinodata.bsm.center.util.system.SystemContext;

public class UserClient extends HessianServlet implements IUser {

    private static final Logger logger = Logger.getLogger(UserClient.class);

    @Override
    public void init() throws ServletException {
        super.init();
        ///////////////////////////////////////////////////////////
        try {
            String realPath = super.getServletContext().getRealPath("/");
            String connect = realPath + "WEB-INF/conf/application.properties";
            Properties prop = new Properties();
            prop.load(new FileInputStream(new File(connect)));
            String sysId = prop.getProperty(FunctionConstants.SYSTEM_ID_KEY);
            String sysName = prop.getProperty(FunctionConstants.SYSTEM_NAME_KEY);
            SystemContext.setProperty(FunctionConstants.SYSTEM_ID_KEY, sysId == null ? "sino-bsm" : sysId);
            SystemContext.setProperty(FunctionConstants.SYSTEM_NAME_KEY, sysName == null ? "综合监控系统" : sysName);
            new UAMDataSource().init(prop);
            UAMFascade.loadFactory();
            UAMFascade.putUAMRequest(new UAMRequest());
            String id = prop.getProperty("sys.id");
            String url = prop.getProperty("sys.url");
            try {
                List<Map<String, Object>> list = UAMDBManager.getInst().queryList("sys.select", new Object[] { id });
                if (list.size() == 0) {
                    UAMDBManager.getInst().update("sys.insert", new Object[] { id, url });
                } else {
                    UAMDBManager.getInst().update("sys.update", new Object[] { url, id });
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            SystemContext.setProperty(FunctionConstants.SYSTEM_ID_KEY, "sino-bsm");
            SystemContext.setProperty(FunctionConstants.SYSTEM_NAME_KEY, "综合监控系统");
            logger.error("failed to read uam conf", e);
        }
        ////////////////////////////////////////////////////////////////////////

    }

    @Override
    public void putDepartment(Department[] departments) {
        UAMFascade.getDepartmentFactory().put(departments);
    }

    @Override
    public void putRole(Role[] Roles) {
        UAMFascade.getRoleFactory().put(Roles);

    }

    @Override
    public void putUserInfo(UserInfo[] users) {
        UAMFascade.getUserInfoFactory().put(users);
    }

    @Override
    public void removeDepartment(Integer[] depIds) {
        UAMFascade.getDepartmentFactory().remove(depIds);
    }

    @Override
    public void removeDptUserRel(DptUserRel[] rels) {
        UAMFascade.getUserInfoFactory().removeDptUserRel(rels);

    }

    @Override
    public void removeRole(Integer[] roleIds) {
        UAMFascade.getRoleFactory().remove(roleIds);
    }

    @Override
    public void removeUserInfo(String[] userIds) {
        UAMFascade.getUserInfoFactory().remove(userIds);
    }

    @Override
    public void removeUserRoleRel(UserRoleRel[] rels) {
        UAMFascade.getUserInfoFactory().removeUserRoleRel(rels);
    }

    @Override
    public void saveDptUserRel(DptUserRel[] rels) {
        UAMFascade.getUserInfoFactory().saveDptUserRel(rels);

    }

    @Override
    public void saveUserRoleRel(UserRoleRel[] rels) {
        UAMFascade.getUserInfoFactory().saveUserRoleRel(rels);

    }

    @Override
    public List<Privilege> getPrivileges() {
        List<Privilege> privleges = UAMFascade.getUAMRequest().getPrivileges();
        return privleges;
    }

    @Override
    public List<Resource> getResourceByParent(String parentId) {
        return UAMFascade.getUAMRequest().getResourceByParent(parentId);
    }

    @Override
    public List<Resource> getRootResource() {
        return UAMFascade.getUAMRequest().getRootResource();
    }

    public String testConnect() {
        return "it works!";
    }

}
